import 'package:flutter/material.dart';
import 'package:flowerny/model/goodDetail.dart';
import 'package:dio/dio.dart';
import 'package:flowerny/base/config/conf_web.dart';

class GoodDetailPage extends StatefulWidget {
  int _id;
  String _good_name;
  GoodDetailPage(int id, String good_name) {
    _id = id;
    _good_name = good_name;
  }

  @override
  State createState() {
    return DetailState(_id, _good_name);
  }
}

class DetailState extends State<GoodDetailPage> {
  int _goodId;
  String _goodName;
  Size screenSize;
  DetailState(int id, String good_name) {
    _goodId = id;
    _goodName = good_name;
  }
  Future<GoodDetail> _fetchGoodDetail() async {
    GoodDetail _goodDetail = null;
    try {
      final response = await Dio().get('${Config.BASE_URL}/goods/get_good',
          queryParameters: {"id": _goodId});
      if (response.statusCode == 200) {
        print('GoodDetail->' + response.toString());
        _goodDetail = GoodDetail.fromJson(response.toString());
        return _goodDetail;
      }
    } catch (e) {
      print('GoodDetail->' + e.toString());
    }
    return _goodDetail;
  }

  @override
  Widget build(BuildContext context) {
    screenSize = MediaQuery.of(context).size;
    return Scaffold(
      body: Stack(
        children: <Widget>[
          loadDetail(),
          SafeArea(
            child: SizedBox(
              width: 50,
              height: 50,
              child: IconButton(
                icon: const Icon(Icons.arrow_back),
                color: Colors.white,
                tooltip: 'Back',
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget loadDetail() {
    return Container(
          child: FutureBuilder(
            future: _fetchGoodDetail(),
            builder: (context, AsyncSnapshot snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                case ConnectionState.waiting:
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                default:
                  if (snapshot.hasError) {
                    return Text('Error:${snapshot.error}');
                  } else {
                    return _createView(context, snapshot);
                  }
              }
            },
          ),
    );
  }

  Widget _createView(BuildContext context, AsyncSnapshot snapshot) {
    GoodDetail _goodDetail = snapshot.data;
    return SingleChildScrollView(
        child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          constraints: const BoxConstraints.expand(height: 248),
          child: Image.network(
            _goodDetail.product_img,
            fit: BoxFit.cover,
          ),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Expanded(
              child: Container(
                padding: const EdgeInsets.all(10.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Text(
                      _goodDetail.product_name,
                      style: new TextStyle(color: Colors.black, fontSize: 18),
                    ),
                    const SizedBox(height: 10),
                Row(
                  children: <Widget>[
                    Text(
                      '积分(包邮)：',
                      style: new TextStyle(
                          color: Colors.grey,
                          fontSize: 12),
                    ),
                    const SizedBox(width: 5),
                    Text(
                      '${_goodDetail.product_price}',
                      style: new TextStyle(
                          color: Color.fromARGB(255, 0, 215, 198),
                          fontWeight: FontWeight.bold,
                          fontSize: 18),
                    ),
                  ]),
                    const SizedBox(height: 10),
                    Row(
                        children: <Widget>[
                          CircleAvatar(
                            backgroundImage: NetworkImage(
                              _goodDetail.product_icon,
                            ),
                            radius: 16,
                          ),
                          const SizedBox(width: 12),
                            Text(
                              '月销量：${_goodDetail.sale_count}',
                              overflow: TextOverflow.ellipsis,
                              maxLines: 1,
                              style: new TextStyle(
                                color: Colors.grey[400],
                                fontSize: 12,
                              ),
                            ),
                          const SizedBox(width: 20),
                          Container(
                            decoration: new BoxDecoration(
                              color: new Color(0xFFF6F6F8),
                              borderRadius: new BorderRadius.all(
                                  new Radius.circular(6.0)),
                            ),
                            padding: const EdgeInsets.only(
                                top: 3.0, bottom: 3.0, left: 8.0, right: 8.0),
                            child: Text(
                              _goodDetail.type_name,
                              style: new TextStyle(
                                  color: Color.fromARGB(255, 0, 215, 198),
                                  fontSize: 13),
                            ),
                          ),
                        ]),
                    const SizedBox(height: 5),
                    Text(
                      _goodDetail.product_detail,
                      style: new TextStyle(
                          color: Colors.black87,
                          fontSize: 15),
                    ),
                    const SizedBox(height: 10),
                    Text(
                      '换购说明：积分扣除日起三日内，换购商品发货，保证正品，无质量问题，概不退换。',
                      style: new TextStyle(
                          color: Colors.black87,
                          fontWeight: FontWeight.bold,
                          fontSize: 15),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
        Container(
          alignment: Alignment(0, -1),
          padding: const EdgeInsets.only(
              left: 5.0, top: 8.0, right: 5.0, bottom: 2.0),
          child: MaterialButton(
            minWidth: screenSize.width*0.9,
            onPressed: () {
              action(_goodDetail.product_type, _goodDetail.mall_url);
            },
            color: Color.fromARGB(255, 0, 215, 198),
            child: Text(
              '立即换购',
              style: TextStyle(
                color: Colors.white,
              ),
            ),
          ),
        ),
      ],
    ));
  }

  void action(int type, String out_url) {}
}
